package com.mef.smsplat.web.controller.sys.group;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.CompanyInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.CompanyInfoService;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/company")
public class CompanyInfoController {
	private CompanyInfoService companyInfoService;

	public void setCompanyInfoService(CompanyInfoService companyInfoService) {
		this.companyInfoService = companyInfoService;
	}

	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<CompanyInfoDomain> query(String companyName,String office,String personPhone,String groupId,DataGridModel dgm) {
		return companyInfoService.query(companyName, office, personPhone,groupId, dgm);
	}

	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(CompanyInfoDomain company) {
		if (company.getCompanyName() == null || "".equals(company.getCompanyName().trim())) {
			throw new BusinessException("公司姓名不能为空");
		}
		if (company.getPersonPhone() == null || "".equals(company.getPersonPhone().trim())) {
			throw new BusinessException("手机号码不能为空");
		}

		company.setCompanyId(IDUtil.next() + "");
		companyInfoService.save(company);
		return new Json();
	}

	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(CompanyInfoDomain company) {
		if (company.getCompanyName() == null || "".equals(company.getCompanyName().trim())) {
			throw new BusinessException("公司姓名不能为空");
		}
		if (company.getPersonPhone() == null || "".equals(company.getPersonPhone().trim())) {
			throw new BusinessException("手机号码不能为空");
		}
		companyInfoService.update(company);
		return new Json();
	}

	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids) {
		//删除company
		companyInfoService.removeById(ids);
		//删除company group关联记录
		companyInfoService.deleteRelateGroup(ids);
		return new Json();
	}

}
